import request from '@/utils/request'
import Qs from 'qs'

export function getEnvironment(size, page, kw) {
    let params = ''
    params += (size ? (params === '' ? '' : '&') + 'size=' + size : '')
    params += (page ? (params === '' ? '' : '&') + 'page=' + page : '')
    params += (kw ? (params === '' ? '' : '&') + 'kw=' + kw : '')
    if (params !== '') {
        params = '?' + params
    }
    return request({
        url: '/environment/' + params,
        method: 'get'
    })
}

export function addEnvironment(envName) {
    return request({
        url: '/environment/',
        method: 'post',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {
            env_name: envName
        }
    })
}

export function updateEnvironment(id, envName, spaceId, status) {
    return request({
        url: '/environment/' + id,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {
            env_name: envName,
            space_id: spaceId,
            status: status
        }
    })
}

export function deleteEnvironment(id) {
    return request({
        url: '/environment/' + id,
        method: 'delete',
        headers: { 'Content-Type': 'application/json' }
    })
}